package com.zhouyibike.uu.utils;

import android.content.Context;
import android.text.ClipboardManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.style.AbsoluteSizeSpan;
import android.util.Log;
import android.widget.EditText;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TextUtils {

	private static final String TAG = TextUtils.class.getSimpleName();
	private static Map<String, MessageDigest> digests = new ConcurrentHashMap<String, MessageDigest>();

	public static boolean isNullorEmpty(String str) {
		if (str == null || str.trim().length() == 0) {
			return true;
		}
		return false;
	}

	public static String serializeArray(String[] array, char split) {
		return serializeCollection(Arrays.asList(array), ',');
	}

	public static String serializeArray(String[] array, char split, String prefix, String subfix) {
		return serializeCollection(Arrays.asList(array), ',', prefix, subfix);
	}

	public static String serializeCollection(Collection<String> array, char split) {
		return serializeCollection(array, split, "", "");
	}

	public static String serializeCollection(Collection<String> array, char split, String prefix, String subfix) {
		StringBuilder sb = new StringBuilder();
		for (String str : array) {
			sb.append(prefix).append(str).append(subfix).append(split);
		}
		sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

	/**
	 * 使用java正则表达式去掉多余的.与0
	 * 
	 * @param s
	 * @return
	 */
	public static String subZeroAndDot(String s) {
		if (s.indexOf(".") > 0) {
			s = s.replaceAll("0+?$", "");// 去掉多余的0
			s = s.replaceAll("[.]$", "");// 如最后一位是.则去掉
		}
		return s;
	}

	public static String MD5(String data) {
		try {
			if (!TextUtils.isNullorEmpty(data)) {
				String md5 = hash(data.toUpperCase().getBytes("UTF-8"), "MD5");
				return md5.substring(5, 29);
			}
		} catch (UnsupportedEncodingException e) {
			Log.e(TAG, e.getMessage());
		}
		return data;
	}
	public static String MD52(String data) {
		try {
			if (!TextUtils.isNullorEmpty(data)) {
				String md5 = hash(data.toUpperCase().getBytes("UTF-8"), "MD5");
				return md5;
			}
		} catch (UnsupportedEncodingException e) {
			Log.e(TAG, e.getMessage());
		}
		return data;
	}

	public static String hash(byte[] bytes, String algorithm) {
		synchronized (algorithm.intern()) {
			MessageDigest digest = digests.get(algorithm);
			if (digest == null) {
				try {
					digest = MessageDigest.getInstance(algorithm);
					digests.put(algorithm, digest);
				} catch (NoSuchAlgorithmException nsae) {
					return null;
				}
			}
			// Now, compute hash.
			digest.update(bytes);
			return encodeHex(digest.digest());
		}
	}

	public static String encodeHex(byte[] bytes) {
		StringBuilder buf = new StringBuilder(bytes.length * 2);
		int i;

		for (i = 0; i < bytes.length; i++) {
			if (((int) bytes[i] & 0xff) < 0x10) {
				buf.append("0");
			}
			buf.append(Long.toString((int) bytes[i] & 0xff, 16));
		}
		return buf.toString();
	}

	public static String subString(String str, int size) {
		if (str.length() <= size) {
			return str;
		}
		return str.substring(0, size) + "...";
	}

	// 实现文本复制功能
	public static void copy(String content, Context context) {
		// 得到剪贴板管理器
		ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
		cmb.setText(content.trim());
	}

	/**
	 * 判断字符串中是否包含字母+任意字符组成 位数6-18位
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isContainNumberLetter(String str) {
		boolean result = false;
		if (!android.text.TextUtils.isEmpty(str)) {
			String s = "^(?=.*?\\d)(?=.*?\\D).{6,18}$";
			result = str.matches(s);
		}
		return result;
	}

	/**
	 * 将11手机号修改为中间四位为****
	 * 
	 * @param mobile
	 *            手机号
	 * @return
	 * 
	 */
	public static String toFormatPhone(String mobile) {
		if (!TextUtils.isNullorEmpty(mobile)) {
			String str1 = mobile;
			StringBuffer str = new StringBuffer(str1);
			str.replace(3, 7, String.valueOf("****"));
			return str.toString();
		} else {
			return mobile;
		}
	}
	public static String toFormatPhone2(String mobile) {
		if (!TextUtils.isNullorEmpty(mobile)) {
			String str1 = mobile;
			StringBuffer str = new StringBuffer(str1);
			str.replace(2, 9, String.valueOf("*******"));
			return str.toString();
		} else {
			return mobile;
		}
	}

	/**
	 * 单独设置edittext hint字号大小，不影响text字号设置。
	 * 
	 * @param et
	 *            控件et
	 * @param hint
	 *            hint显示内容
	 * @param textsize
	 *            hint字号尺寸
	 */
	public static void setEtHint(EditText et, CharSequence hint, int textsize) {
		// 新建一个可以添加属性的文本对象
		SpannableString ss = new SpannableString(hint);
		// 新建一个属性对象,设置文字的大小
		AbsoluteSizeSpan ass = new AbsoluteSizeSpan(textsize, true);
		// 附加属性到文本
		ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		// 设置hint
		et.setHint(new SpannedString(ss)); // 一定要进行转换,否则属性会消失
	}
}
